package cn.lingyangwl.agile.pay.mch.controller;

import cn.lingyangwl.framework.core.response.Resp;
import cn.lingyangwl.agile.pay.model.entity.MchApp;
import cn.lingyangwl.agile.pay.model.rqrs.MchAppQL;
import cn.lingyangwl.agile.pay.model.rqrs.MchAppSaveRQ;
import cn.lingyangwl.agile.pay.model.rqrs.MchAppUpdateRQ;
import cn.lingyangwl.agile.pay.common.service.MchAppService;
import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shenguangyang
 */
@RestController
@RequestMapping("/mchApp")
public class MchAppController {
    @Resource
    private MchAppService mchAppService;

    @RamResource(action = "pay:mchApp:query", name = "查询商户应用", type = ActionType.READ)
    @GetMapping("/page")
    public Resp<IPage<MchApp>> page(MchAppQL query) {
        IPage<MchApp> page = mchAppService.pageMchApp(query);
        return Resp.ok(page);
    }

    @RamResource(action = "pay:mchApp:query", name = "查询商户应用", type = ActionType.READ)
    @GetMapping("/listAllMchApp")
    public Resp<List<MchApp>> listAllMchApp() {
        List<MchApp> resp = mchAppService.listAllMchApp();
        return Resp.ok(resp);
    }

    @RamResource(action = "pay:mchApp:save", name = "保存商户应用", type = ActionType.WRITE)
    @PostMapping("/save")
    public Resp<?> save(@RequestBody @Validated MchAppSaveRQ req) {
        mchAppService.saveMchApp(req);
        return Resp.ok();
    }

    @RamResource(action = "pay:mchApp:update", name = "更新商户应用", type = ActionType.WRITE)
    @PostMapping("/update")
    public Resp<?> update(@RequestBody MchAppUpdateRQ req) {
        mchAppService.updateMchApp(req);
        return Resp.ok();
    }

    @RamResource(action = "pay:mchApp:delete", name = "删除商户应用", type = ActionType.WRITE)
    @DeleteMapping("/delete")
    public Resp<?> delete(@RequestParam("appId") String appId) {
        mchAppService.deleteByAppId(appId);
        return Resp.ok();
    }

    @GetMapping("/getById")
    @RamResource(action = "pay:mchApp:query", name = "查询商户应用", type = ActionType.READ)
    public Resp<MchApp> get(@RequestParam("id") String id) {
        MchApp mchApp = mchAppService.getMchAppById(id);
        return Resp.ok(mchApp);
    }
}
